<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class RegionManager {
	
	private $db_access;

	function __construct() {
		$this->db_access = new DbAccessor();
	}

	function region_get_regions() {
		$sqlresult = $this->db_access->db_do_query(
				$this->db_access->db_query_select(
						array(
							array(), "REGION.*, REGION_TREE.*"), 
							array("REGION INNER JOIN REGION_TREE ON region_id = sub_region", "DUMMY"), 
							1, 
							"parent_region ASC, name ASC"));
		$rs = array();
		while ($r = $this->db_access->db_fetch_array($sqlresult)) {
			$rs[$r['region_id']] = $r;
			$rs[$r['parent_region']]['children'][] = $r['region_id'];
		}
		return $rs;
	}

	function region_get_region_tree($region_id, $tree=0) {
		if (!$tree) {
			$tree = $this->region_get_regions();
		}
		$r = array();
		while ($region_id > 0) {
			$r[] = $tree[$region_id];
			$region_id = $tree[$region_id]['parent_region'];
		}
		return $r;
	}

	function region_get_regions_kv($insertmode=0, $tree=0) {
		if (!$tree) {
			$tree = $this->region_get_regions();
		}
		$k = array();
		$v = array();
		return $this->region_get_regions_kv_aux($insertmode, $tree, $k, $v, 0, 0);
	}

	function region_get_regions_kv_aux($insertmode, $tree, $keys, $values, $parent, $level) {
		$space = "";
		for ($i = 0; $i < $level; $i++) {
			$space .= "&nbsp;&nbsp;&nbsp;&nbsp;";
		}
		reset($tree[$parent]['children']);
		do {
			$id = current($tree[$parent]['children']);
			$keys[] = $id;
			$values[] = $space . $tree[$id]['name'];
			if (is_array($tree[$id]['children']) and count($tree[$id]['children'])) {
				$kv = $this->region_get_regions_kv_aux($insertmode, $tree, $keys, $values, $id, $level+1);
				$keys = $kv['keys'];
				$values = $kv['values'];
			}
			if ($insertmode) {
				$keys[] = "parent" . $id;
				$values[] = $space . "&nbsp;&nbsp;&nbsp;&nbsp;[in {$tree[$id]['name']}]";
			}
		} while (next($tree[$parent]['children']));		
		return array('keys' => $keys, 'values' => $values);
	}

	function region_format($regio) {
		$str = "";
		if (is_array($regio) and count($regio)) {
			while ($r = @array_pop($regio)) {
				$str = "$str > {$r['name']}";
			}
		} else {
			return "[onbekend]";
		}
		return substr($str, 3);
	}

	function region_is_inside($target, $super, $tree=0) {
		if ($tree == 0) {
			$tree = $this->region_get_regions();
		}

		$target = $this->region_get_region_tree($target, $tree);
		while ($r = array_shift($target)) {
			if ($super == $r['region_id']) {
				return true;
			}
		}
		return false; 
	}
}

?>